<?php
require_once(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR ."core".DIRECTORY_SEPARATOR."functions.php");
Core::getResource("Model.Person");
Core::getResource("Controller.baseController");

class VwallController extends baseController
{
	private $personId;
	private $person;//Person Model Class
	private $message;
	public function __construct($personId)
	{
		$this->personId = $personId;
		$this->person = new Person($this->personId);
	}
	

	private function match_password($password)
	{
		return ($this->person->password == $password);
	}

	public function changePassword($oldPassword,$newPassword)
	{	
		$oldPassword = md5($oldPassword.$this->person->salt);
		$newPassword = md5($newPassword.$this->person->salt);
		if($this->match_password($oldPassword))
		{
			$this->person->setPassword($newPassword);	
			return true;
		}
		else
		{
			return false;
		}
	}

	public function personActivated()
	{
		return $this->person->activated;
	}

	public function addMessage($message)
	{
		$this->message .= $message;
	}
    
    public function getActivationKey()
    {
        return $this->person->activationKey;
    }

	public function clearMessages()
	{
		$this->message = null;
	}
	
	public function printMessages()
	{
		echo $this->message;
		$this->clearMessages();
	}

	public function print_details()
	{
		echo "logged in as : ";
		echo "".$this->person->email;
		echo "<br/>";
		echo "Fullname : ".$this->person->firstName;
		echo " ".$this->person->lastName."<br/>";
		echo "Gender : ".$this->person->gender."<br/>";
		echo "Contact No : ".$this->person->contactNumber."<br/>";
		echo $this->person->password."<br/>";
		echo "salt : ".$this->person->salt."<br/>";
		echo $this->person->accountType;
	}

	public function printName()
	{
		echo $this->person->firstName." ".$this->person->lastName;
	}

	public function hasAccess()
	{
		return ($this->person->isAdmin() || $this->person->isEvtMgr());
	}

	public function isUserAdmin()
	{
		return $this->person->isAdmin();
	}

	public function isEvtMgr()
	{
		return $this->person->isEvtMgr();
	}

	public function matchAccountType($accountType)
	{
		return ($this->person->accountType == $accountType);
	}

	public function getAccountType()
	{
		return $this->person->accountType;
	}

	public function getJoinDate()
	{
		$timestamp = strtotime($this->person->dateCreated);

		return date("Y",$timestamp);
	}

	public function personFName()
	{
		return $this->person->firstName;
	}

	public function personLName()
	{
		return $this->person->lastName;
	}

	public function personEmail()
	{
		return $this->person->email;
	}

	public function personSkills()
	{
		return ($this->person->skills == "NULL" ? "None" : $this->person->skills);
	}

	public function personGender()
	{
		return $this->person->gender;
	}

	public function personContactNumber()
	{
		return $this->person->contactNumber;
	}

	public function personAddress()
	{
		return ($this->person->address == "NULL" ? "None" : $this->person->address);
	}

	public function personMemberSince()
	{
		return ($this->person->dateCreated);
	}

	public function uniId()
	{
		return $this->person->universityId;
	}

	public function match($var,$varname)
	{
		return ($var==$this->person->{$varname});
	}

	public function updateRoleApplied($personId,$roleId,$attended,$hoursWorked)
	{
		Role::updateRoleApplied($personId,$roleId,$attended,$hoursWorked);
	}

	public function updateProfile($firstName,$lastName,$accountType,$email,$skills,$gender,$contactNumber,$address,$sendEmail)
	{
		if($sendEmail)
		{
            $res = $this->person->updateProfile($firstName,$lastName,$accountType,$email,$skills,$gender,$contactNumber,$address, false);
			$this->person->setActivated(0);
			core::sendMail($this->person->email,"Email Update Confirmation","Your email has been changed.<br/> To confirm enter this activation key when promted after login.<br/><br/><h2>Activation Key : ".$this->person->activationKey."</h2>");
		}
        else
        {
            $res = $this->person->updateProfile($firstName,$lastName,$accountType,$email,$skills,$gender,$contactNumber,$address, true);
        }
	}

	//$id, $studentNumber, $degreeName, $majors, $yod, $entryToUts, $highSchool, $eca, $scholarShips, $previousInternships, $internshipOrganized

	public function updateUniversityDetails($universityId,$degreeName,$majors,$yod,$entryToUts,$highSchool,$eca,$scholarShips,$previousInternships,$internshipOrganized)
	{
		$this->person->updateUniversityDetails($universityId,$degreeName,$majors,$yod,$entryToUts,$highSchool,$eca,$scholarShips,$previousInternships,$internshipOrganized);
	}
    
    public function updateEditProfileOtherDetails($availaibity,$workshops,$driversLicence,$refugee,$indigenous)
	{
		$this->person->updateEditProfileOtherDetails($availaibity,$workshops,$driversLicence,$refugee,$indigenous);
	}

	public function getAllPersonIdName()
	{
		return $this->person->getAllPersonIdName();
	}
}

?>